/*
 * animation.cpp
 *
 *  Created on: 28-apr-2009
 *      Author: fabrizio
 */
/*

 Copyright (c) 2008-2009 Fabrizio Pistonesi

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */

#include "animation.h"

Animation::Animation(IrrlichtDevice *device, u32 life, vector3df rotation, stringc sprites, s32 tpf, bool loop)
{
  ISceneManager *smgr = Device->getSceneManager();
  if(life > 0)
    {
      Life = life;
      IrrAnimation.push_back(smgr->createDeleteAnimator(Life));
    }
  if(rotation != vector3df(0,0,0))
    {
      Rotation = rotation;
      IrrAnimation.push_back(smgr->createRotationAnimator(Rotation));
    }
  if(sprites != "")
    {
      Sprites = sprites.c_str();
      IFileSystem *fs = Device->getFileSystem();
      stringc wd = fs->getWorkingDirectory();
      fs->changeWorkingDirectoryTo(sprites.c_str());
      IFileList *fl = fs->createFileList();
      fs->changeWorkingDirectoryTo(wd.c_str());
      array<ITexture*> texture;
      for(int i = 1; i < (int)fl->getFileCount(); i++)
        {
          if(!fl->isDirectory(i))
            {
              texture.push_back(Device->getVideoDriver()->getTexture(fl->getFileName(i)));
            }
        }
      if(tpf > 0) TimePerFrame = tpf;
      Loop = loop;
      IrrAnimation.push_back(smgr->createTextureAnimator(texture,tpf,loop));
    }
  if(life > 0)
    {
      Life = life;
      IrrAnimation.push_back(smgr->createDeleteAnimator(Life));
    }

}

void Animation::serialize(IAttributes *attributes)
{
	attributes->addInt("life",Life);
	attributes->addVector3d("rotation",Rotation);
	attributes->addString("sprites",Sprites.c_str());
	attributes->addInt("timeperframe",TimePerFrame);
	attributes->addBool("loop",Loop);
}

Animation::~Animation()
{

}
